package org.op.nav;

import java.util.List;

import javax.swing.tree.TreeModel;
import javax.swing.tree.TreeNode;

import org.op.FrameView;
import org.op.editor.doc.EditorLoadController;
import org.op.service.WwDocumentInfo;
import org.op.service.WwInfoObject;
import org.op.service.config.ConfigKeys;
import org.op.service.config.ConfigurationService;
import org.op.service.fileio.FileRWService;
import org.op.service.log.LoggerService;
import org.op.service.nav.FileNavigationService;
import org.op.util.MessageController;

public class NavigationControllerImpl implements NavigationController {
	
	private FileNavigationService fileNavService;
	private FileRWService fileRWService;
	private EditorLoadController editorController;
	private NavigationView navView;
	private ConfigurationService configService;
	private MessageController messageController;
	private FrameView frameView;
	private LoggerService logger;
	
	public NavigationControllerImpl() {
		
	}
	
	/**
	 *  Create the tree model
	 * 
	 */
	public TreeModel loadTreeModel(TreeModel model) {
		try {
			String workspace = configService.getApplicationProperties().getProperty(ConfigKeys.KEY_CURRENT_WORKSPACE);
			model = fileNavService.loadTreeRoot(model, workspace);
		} catch (Exception e) {
			// TODO - throw exception to frame
			e.printStackTrace();
		}

		return model;
	}
	
	public void loadNewTree() {
		TreeModel tm = loadTreeModel(null);
		navView.updateModel(tm);
	}
	
	public void reloadTree() {
		TreeModel tm = navView.getModel();
		tm = loadTreeModel(tm);
		navView.updateModel(tm);  // not necessary
	}

	public void openNode(TreeNode node) {
		
		try {
			WwInfoObject wwInfo = fileNavService.getAssociatedInfo(node);
			if (wwInfo instanceof WwDocumentInfo) {
				String associatedFile = ((WwDocumentInfo)wwInfo).getFilepath();
				if (associatedFile != null) {
					List<String> file = fileRWService.readFileText(associatedFile);
					editorController.loadFileIntoEditor(file, (WwDocumentInfo) wwInfo);
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	
	public ConfigurationService getConfigService() {
		return configService;
	}

	public void setConfigService(ConfigurationService configService) {
		this.configService = configService;
	}

	public FileNavigationService getFileNavService() {
		return fileNavService;
	}

	public void setFileNavService(FileNavigationService fileNavService) {
		this.fileNavService = fileNavService;
	}

	public FileRWService getFileRWService() {
		return fileRWService;
	}

	public void setFileRWService(FileRWService fileRWService) {
		this.fileRWService = fileRWService;
	}

	public EditorLoadController getEditorController() {
		return editorController;
	}

	public void setEditorController(EditorLoadController editorController) {
		this.editorController = editorController;
	}

	public NavigationView getNavView() {
		return navView;
	}

	public void setNavView(NavigationView navView) {
		this.navView = navView;
	}

	public MessageController getMessageController() {
		return messageController;
	}

	public void setMessageController(MessageController messageController) {
		this.messageController = messageController;
	}

	public FrameView getFrameView() {
		return frameView;
	}

	public void setFrameView(FrameView frameView) {
		this.frameView = frameView;
	}

	public LoggerService getLogger() {
		return logger;
	}

	public void setLogger(LoggerService logger) {
		this.logger = logger;
	}




}
